package com.chao.mail.application

import com.chao.mail.api.dto.MailSendMessage
import com.chao.mail.api.dto.MailSendResponse
import com.chao.mail.core.Mail
import com.chao.mail.core.interfaces.IMailRepository
import com.chao.mail.core.interfaces.IMailSender
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

@Service
class MailApplicationService {
    @Autowired
    lateinit var mailSender: IMailSender
    @Autowired
    lateinit var mailRepository: IMailRepository

    fun sendMail(sendMail: MailSendMessage): MailSendResponse {
        val mail = Mail(
                from = sendMail.from,
                to = sendMail.to,
                subject = sendMail.subject,
                text = sendMail.text,
                cc = sendMail.cc,
                bcc = sendMail.bcc,
                multipartFiles = sendMail.multipartFiles,
                sentDate = sendMail.sentDate
        )
        mail.mailSender = mailSender
        mail.mailRepository = mailRepository
        mail.sendMail()
        mail.saveMail()
        return MailSendResponse(id = mail.id, from = mail.from, to = mail.to)
    }
}